package study.db.postgresql.controller;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Slf4j
public class SeatNoMger {

    private BlockingDeque<String> queue = new LinkedBlockingDeque();

    private int totalSeatSize;

    public SeatNoMger(List<ExamRoomInfo> roomInfoLt) {
        totalSeatSize = roomInfoLt.stream().mapToInt(ExamRoomInfo::getSeatSize).sum();
        Collections.shuffle(roomInfoLt);
        for (ExamRoomInfo roomInfo : roomInfoLt) {
            String room = roomInfo.getExamRoom();
            int size = roomInfo.getSeatSize();
            List<Integer> seatNoLt = IntStream.range(1, size + 1)
                    .boxed()
                    .collect(Collectors.toList());
            Collections.shuffle(seatNoLt);
            seatNoLt.forEach(seatNo -> queue.add(StrUtil.format("{},{}", room, seatNo)));
        }
    }

    public String get() {
        if (queue.isEmpty()) {
            throw new IllegalStateException(StrUtil.format("超过最大座位数: {}", totalSeatSize));
        }
        return queue.pop();
    }


    public static void main(String[] args) {
        List<ExamRoomInfo> roomInfoLt = Lists.newArrayList();
        //
        ExamRoomInfo info1 = new ExamRoomInfo();
        info1.setExamRoom("测试考场1");
        info1.setSeatSize(5);
        roomInfoLt.add(info1);
        //
        ExamRoomInfo info2 = new ExamRoomInfo();
        info2.setExamRoom("测试考场2");
        info2.setSeatSize(10);
        roomInfoLt.add(info2);


        SeatNoMger utils = new SeatNoMger(roomInfoLt);

        for (int i = 1; i <= 13; i++) {
            log.info("{}", utils.get());
        }
    }
}
